﻿Imports System.Runtime.Serialization

<Serializable()> _
Public Class User
    Implements ISerializable


#Region "Properties"

    Private _employeeCode As Integer
    Public Property EmployeeCode() As Integer
        Get
            Return _employeeCode
        End Get
        Set(ByVal value As Integer)
            _employeeCode = value
        End Set
    End Property

    Private _username As String
    Public Property Username() As String
        Get
            Return _username
        End Get
        Set(ByVal value As String)
            _username = value
        End Set
    End Property

    <DebuggerBrowsable(DebuggerBrowsableState.Never)> _
    Private _password As String

    ''' <summary>
    ''' Gets/sets the user's sensitive property - the password.
    ''' </summary>
    ''' <remarks>This property will not be shown in debug mode.</remarks>
    <DebuggerBrowsable(DebuggerBrowsableState.Never)> _
    Public Property Password() As String
        Get
            Return _password
        End Get
        Set(ByVal value As String)
            _password = value
        End Set
    End Property

    Private _update As Date
    Public Property DateChanged() As Date
        Get
            Return _update
        End Get
        Set(ByVal value As Date)
            _update = value
        End Set
    End Property

    Private _authorityCode As Integer
    Public Property AuthorityCode() As Integer
        Get
            Return _authorityCode
        End Get
        Set(ByVal value As Integer)
            _authorityCode = value
        End Set
    End Property

#End Region

#Region "Creation"

    Protected Sub New()

    End Sub

    Public Sub New(ByVal info As SerializationInfo, _
                   ByVal context As StreamingContext)
        Me.Username = info.GetString("Username")
        Me.Password = info.GetString("Password")
        Me.DateChanged = info.GetDateTime("DateChanged")
        Me.EmployeeCode = info.GetInt32("EmployeeCode")
        Me.AuthorityCode = info.GetInt32("AuthorityCode")

    End Sub

    Public Sub GetObjectData(ByVal info As SerializationInfo, _
                             ByVal context As StreamingContext) _
                             Implements ISerializable.GetObjectData
        info.AddValue("Username", Me.Username)
        info.AddValue("Password", Me.Password)
        info.AddValue("DateChanged", Me.DateChanged)
        info.AddValue("EmployeeCode", Me.EmployeeCode)
        info.AddValue("AuthorityCode", Me.AuthorityCode)
    End Sub

    Public Shared Function CreateNewUser() As User
        Return New User()
    End Function

    Public Shared Function CreateUser(ByVal username As String, _
                                      ByVal password As String, _
                                      ByVal update As Date, _
                                      ByVal code As Integer, _
                                      ByVal authority As Integer) As User
        Dim user As User = New User()
        With user
            user.Username = username
            user.Password = password
            user.DateChanged = update
            user.EmployeeCode = code
            user.AuthorityCode = authority
        End With

        Return user
    End Function

#End Region

#Region "Overriden Object"

    Public Overrides Function GetHashCode() As Integer
        Dim hashCode As Integer = 0
        hashCode += 1000000001 * Username.GetHashCode()
        hashCode += 1000000002 * Password.GetHashCode()
        hashCode += 1000000003 * DateChanged.GetHashCode()
        hashCode += 1000000004 * EmployeeCode.GetHashCode()
        hashCode += 1000000005 * AuthorityCode.GetHashCode()
        Return hashCode
    End Function

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Dim other As User = TryCast(obj, User)
        If other Is Nothing Then
            Return False
        End If

        Return Me.Username = other.Username
    End Function

    Public Overrides Function ToString() As String
        Dim result As Text.StringBuilder = New Text.StringBuilder()
        result.Append("Welcome: ")
        result.Append(Me.Username)

        Return result.ToString()
    End Function

#End Region

End Class
